package org.david.code.jdbc;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class ConnFactory {

	/**
	 * @param args
	 */
	private static Connection con;
	private static DatabaseMetaData dbmd;
	private ConnFactory(){
		
	}
	public static Connection createConnection() throws ClassNotFoundException, SQLException{
		if(con!=null){
			return con;
		}else{
			Properties props =new Properties();
			props.put("remarksReporting","true");//oracle必须设置该参数才能取出注释
			props.put("user",DBUser.userName);
			props.put("password",DBUser.password);
			Class.forName(DBUser.driverName); 
			//con=DriverManager.getConnection(DBUser.driverUrl, DBUser.userName, DBUser.password);
			con=DriverManager.getConnection(DBUser.driverUrl, props);
		}
		return con;
	}
	public static void close() throws SQLException{
		if(con!=null){
			con.close();
		}
		else{
			System.out.println("连接不存在或者已关闭");
		}
	}
	public static DatabaseMetaData buildDatabaseMetadata() throws SQLException, ClassNotFoundException{
		if(dbmd==null){
			return ConnFactory.createConnection().getMetaData();
		}
		else{
			return dbmd;
		}
	}
	public static void main(String[] aa) throws SQLException, ClassNotFoundException{
		DatabaseMetaData metadata = createConnection().getMetaData();
		 // table type. Typical types are "TABLE", "VIEW", "SYSTEM   
        // TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS",   
        // "SYNONYM".  
		String[] types = { "TABLE" };   
		//oracle数据库这些条件得大写
        ResultSet rs = metadata.getTables(DBUser.database.toUpperCase(), DBUser.userName.toUpperCase(), "EXPO%", types);   
            while (rs.next())   
            {   
                String tableName = rs.getString("TABLE_NAME");   
                //String tableType = rs.getString("TABLE_TYPE");   
                // 关于表的注释   
                //String remarks = rs.getString("REMARKS");   
                System.out.println(tableName + "	");   
            }   
	}
}